package com.wanlian.router.utils;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.support.v4.content.FileProvider;

import com.smart.android.utils.DateTime;

import java.io.File;
import java.util.Locale;

/**
 * Created by Hyu on 2018/10/17.
 * Email: fvaryu@163.com
 */
public class Utils {

    public static void main(String[] args) {
        // doSomething ...

        long d = System.currentTimeMillis() -  3 * 1000;
        System.out.println(getDate(d));
    }

    public static String getDate(long date) {
        long d = System.currentTimeMillis() - date;
//        long hours = (d % DateTime.TIME_MILLS_ONEDAY) / DateTime.TIME_MILLS_ONEHOUR;

        DateTime dateTime = new DateTime(date);
        if (d >= 7 * DateTime.TIME_MILLS_ONEDAY) {
            return dateTime.getStringDates("yyyy/MM/dd HH:mm");
        }


        if (d >=  DateTime.TIME_MILLS_ONEDAY) {
            return String.format(Locale.getDefault(), "%d天前", d / DateTime.TIME_MILLS_ONEDAY);
        }

        if (d >= DateTime.TIME_MILLS_ONEHOUR) {
            return String.format(Locale.getDefault(), "%d小时前", d / DateTime.TIME_MILLS_ONEHOUR);
        }

        if (d >= 60 * 1000)
            return String.format(Locale.getDefault(), "%d分钟前", d / (60 * 1000));

        return "刚刚";
    }

    public static void install(Activity activity, File file) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.addCategory(Intent.CATEGORY_DEFAULT);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            Uri contentUri = FileProvider.getUriForFile(activity, activity.getApplicationContext().getApplicationInfo().packageName + ".fileprovider", file);
            intent.setDataAndType(contentUri, "application/vnd.android.package-archive");
        } else {
            intent.setDataAndType(Uri.parse("file://" + file.toString()), "application/vnd.android.package-archive");
        }
        activity.startActivity(intent);

    }
}
